#include <stdlib.h>
#include <stdio.h>
#include <typescommon.h>

// #####################task hello begin############################
#define taskIntervalMs_hello 1000
sysTmrType taskLastUpMs_hello = 0;
void taskFuncInit_hello()
{
    logdebug("");
}
void taskFuncUp_hello()
{
    logdebug("");
}
void taskFuncQuit_hello()
{
    logdebug("");
}
// #####################task hello end############################

// #####################task check1 begin############################
#define taskIntervalMs_check1 10000
sysTmrType taskLastUpMs_check1 = 0;
void taskFuncInit_check1()
{
    logdebug("");
}
void taskFuncUp_check1()
{
    logdebug("");
}
void taskFuncQuit_check1()
{
    logdebug("");
}
// #####################task check1 end############################

typedef enum
{
    taskDoId_hello = 0,
    taskDoId_check1,
    taskDoId_Max,
} taskDoIdEnum;

#define TASK_RUN_MAX_MS 5

#define taskEnumGet(tname) taskDoId_##tname
#define taskFuncInitGet(tname) taskFuncInit_##tname
#define taskFuncUpGet(tname) taskFuncUp_##tname
#define taskFuncQuitGet(tname) taskFuncQuit_##tname
#define taskIntervalGet(tname) taskIntervalMs_##tname
#define taskLastUpGet(tname) taskLastUpMs_##tname

void taskListDoInit()
{
#define taskDoOneInit(tname)      \
    case taskEnumGet(tname):      \
    {                             \
        taskFuncInitGet(tname)(); \
        break;                    \
    }
    int i = 0;
    for (i = 0; i < taskDoId_Max; i++)
    {
        switch (i)
        {
            taskDoOneInit(hello);
            taskDoOneInit(check1);
        default:
        {
            break;
        }
        }
    }
}

void taskListDoQuit()
{
#define taskDoOneQuit(tname)      \
    case taskEnumGet(tname):      \
    {                             \
        taskFuncQuitGet(tname)(); \
        break;                    \
    }
    int i = 0;
    for (i = 0; i < taskDoId_Max; i++)
    {
        switch (i)
        {
            taskDoOneQuit(hello);
            taskDoOneQuit(check1);
        default:
        {
            break;
        }
        }
    }
}

void taskListDoUp(uint16_t *upid)
{
    uint16_t i;
    sysTmrType tmpTmMs;
    tmpTmMs = systemNowMs();
#define taskDoOneUp(tname)                                            \
    case taskEnumGet(tname):                                          \
    {                                                                 \
        if (tmpTmMs - taskLastUpGet(tname) >= taskIntervalGet(tname)) \
        {                                                             \
            taskFuncUpGet(tname)();                                   \
            taskLastUpGet(tname) = systemNowMs();                     \
        }                                                             \
        break;                                                        \
    }
    for (i = 0; i < taskDoId_Max; i++)
    {
        upid[0] = CYCLE_ADD(upid[0], i, taskDoId_Max);
        switch (upid[0])
        {
            taskDoOneUp(hello);
            taskDoOneUp(check1);
        default:
        {
            break;
        }
        }
        if (systemNowMs() - tmpTmMs >= TASK_RUN_MAX_MS)
        {
            break;
        }
    }
}

int main(int argc, char **argv)
{
    uint16_t tmpid = 0;
    taskListDoInit();
    while (1)
    {
        taskListDoUp(&tmpid);
        usleep(10000);
    }
    taskListDoQuit();
    return 0;
}